package com.watson.onebox.sc.springcloud;

import com.watson.onebox.bean.Dessert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;

@Controller
@Slf4j
public class FooController {

    @RequestMapping(method = RequestMethod.GET, value = "/foos1/{id}")
    @ResponseBody
    public Foo findById(
            @PathVariable long id, HttpServletRequest req, HttpServletResponse res) {
        return new Foo(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
    }

    @GetMapping("/foos/{id}")
    @ResponseBody
    public Foo findByIdWithFilter(
            @PathVariable long id, HttpServletRequest req, HttpServletResponse res) {
        if (req.getHeader("Test") != null) {
            res.addHeader("Test", req.getHeader("Test"));
        }
        return new Foo(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
    }

    @Autowired
    Dessert dessert;

    @Scheduled(fixedDelay = 1000)
    public void test() {
        log.info("{}, {}, {}, {}, {}", dessert);
    }



}
